using System.Windows;
using System.Windows.Media;

namespace 地图哇哇哇
{
    public class DrawingHelper
    {
        public static GlyphRun GetGlyphRun(string text, double fontsize, GlyphTypeface glyface, Rect srcRect, out double textwidth, bool aligncenter)
        {
            var totalwidth = 0d;
            var l = text.Length;
            var glyphIndexes = new ushort[l];
            var advanceWidths = new double[l];
            try
            {
                for (var i = 0; i < l; i++)
                {
                    var index = glyface.CharacterToGlyphMap[text[i]];
                    glyphIndexes[i] = index;
                    var width = glyface.AdvanceWidths[index] * fontsize;
                    advanceWidths[i] = width;
                    totalwidth += width;
                }
            }
            catch
            {
            }

            textwidth = totalwidth;
            return new GlyphRun(glyface, 0, false, fontsize, glyphIndexes,
                aligncenter
                    ? new Point(srcRect.X + (srcRect.Width - totalwidth) / 2,
                        srcRect.Y + (srcRect.Height + fontsize) / 2)
                    : new Point(), advanceWidths, null, null, null, null, null, null);
        }
    }
}